from PyQt4.QtGui import QWidget
from PyQt4 import QtCore,QtGui
from KCharts.KPlotItem.KSingleChartPlot import KSingleChartPlot
from KCharts.KPlotItem.KChartPriceDisplayItem import KChartPriceDisplayItem
from KCharts.KDisplayItem.KChartBaseDisplayItem import KChartBaseDisplayItem
import pyqtgraph as pg
from pyqtgraph.dockarea import *
import numpy as np
class KSingleChartPricePlot(KSingleChartPlot):
	'''
	subclass of KSingleChartPlot, is responsible for render curve information(such as volumn) on the plot.
	'''
	PeriodChanged=QtCore.pyqtSignal(str)
	
	def __init__(self, parent,stockobject):
		super(KSingleChartPricePlot, self).__init__(parent,stockobject)

		
		item = KChartPriceDisplayItem(stockobject)
		self.plot.clearPlots()
		self.plot.addItem(item)
		
## The only required methods are paint() and boundingRect() 
## (see QGraphicsItem documentation)
class KChartPriceDisplayItem(KChartBaseDisplayItem):
	'''dieplay item specially for price'''
	def __init__(self, stockobject):
		super(KChartPriceDisplayItem,self).__init__(stockobject)

	def PrepareRenderPicture(self):
		## pre-computing a QPicture object allows paint() to run much more quickly, 
		## rather than re-drawing the shapes every time.
		priceblocks=self.m_stock.CurWindowData('Price')
		XCordinates=self.m_stock.CurWindowData('Index')

		self.picture = QtGui.QPicture()
		p = QtGui.QPainter(self.picture)
		p.setPen(pg.mkPen('w'))
		width=1.0/3.0
		for index in range(0,len(priceblocks)):
			(openp,highp,lowp,closep,vol)=priceblocks[index]
			xcord=XCordinates[index]
			if abs(highp-lowp)>0.00001:	# draw high/low when not equal
				p.drawLine(QtCore.QPointF(xcord, lowp), QtCore.QPointF(xcord, highp))

			if openp > closep:
				p.setBrush(pg.mkBrush('r'))		
				
			else:
				p.setBrush(pg.mkBrush('g'))	

			p.drawRect(QtCore.QRectF(xcord-width,closep,width*2, openp-closep))				
		p.end()


		

		